﻿using System;

using Fuse8.DomainFramework.Common;

using Fuse8.ContentManagementFramework.SystemFacade.Utilities;

namespace Fuse8.ContentManagementFramework.SystemFacade.Extensions
{
    internal enum ObjectSerializers
    {
        Xml = 0,
        Jsv = 1,
        Json = 2,
        UniversalBinaryJson = 3,
        ProtoBuf = 4
    }

    public static class EntitySerializator
    {
        private static ObjectSerializers _usedSerializer = ObjectSerializers.Xml; //TO DO

        public static string StoreMe<T>(this T entity) where T : class, new()
        {
            Guard.ArgumentNotNull(entity, "entity");

            string result = null;

            switch (_usedSerializer)
            {
                case ObjectSerializers.Jsv:
                    result = ServiceStackTypeSerializer<T>.Serialize(entity);
                    break;
                case ObjectSerializers.Xml:
                    result = PlainXmlSerializer<T>.Serialize(entity);
                    break;
            }

            return result;
        }

        public static string StoreMe(this object entity, Type typeFrom)
        {
            Guard.ArgumentNotNull(entity, "entity");
            Guard.ArgumentNotNull(typeFrom, "typeFrom");

            return new PlainXmlSerializer(typeFrom).InnerSerialize(entity);
        }

        public static T DigUpFromString<T>(this string storedEntity) where T : class, new()
        {
            Guard.ArgumentNotEmpty(storedEntity, "storedEntity");

            T result = default(T);

            switch (_usedSerializer)
            {
                case ObjectSerializers.Jsv:
                    result = ServiceStackTypeSerializer<T>.Deserialize(storedEntity);
                    break;
                case ObjectSerializers.Xml:
                    result = PlainXmlSerializer<T>.Deserialize(storedEntity);
                    break;
            }

            return result;
        }

        public static object DigUpFromString(this string storedEntity, Type typeFrom)
        {
            Guard.ArgumentNotEmpty(storedEntity, "storedEntity");

            return new PlainXmlSerializer(typeFrom).InnerDeserialize(storedEntity);
        }
    }
}
